package cc.rengu.igas.channel.upqc.core.service.trans;

import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.B2CATMCashNoticeRequest;
import cc.rengu.igas.channel.upqc.facade.response.B2CATMCashNoticeResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 手机银行ATM扫码取款取现操作通知
 *
 * @author panky
 * @version 1.0.0 创建时间：2020/10/20
 */
public class B2CATMCashNoticeService extends PayService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        B2CATMCashNoticeRequest b2CATMCashNoticeRequest = new B2CATMCashNoticeRequest();
        ConvertUtil.convertOutput(b2CATMCashNoticeRequest);
        return b2CATMCashNoticeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        B2CATMCashNoticeResponse b2CATMCashNoticeResponse = new B2CATMCashNoticeResponse();
        B2CATMCashNoticeRequest b2CATMCashNoticeRequest = (B2CATMCashNoticeRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(b2CATMCashNoticeRequest);
        packagePayerInfo(b2CATMCashNoticeRequest.getPayerInfo());
        convertRiskInfo(b2CATMCashNoticeRequest.getRiskInfo());
        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, b2CATMCashNoticeRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, b2CATMCashNoticeRequest.getHeader().getTxnNum());

        if (StringUtil.isEmptyOrNull(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.ORDER_CCY))) {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_CCY, xmlTreeUtil.getXmlTreeStringValue("currencyCode"));
        }

        if (StringUtil.isEmptyOrNull(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.ORDER_AMT))) {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_AMT, xmlTreeUtil.getXmlTreeStringValue("txnAmt"));
        }

        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联ATM取现操作通知服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            b2CATMCashNoticeResponse.setRespCode("04");
            b2CATMCashNoticeResponse.setRespDesc("交易状态未明");
        } else if (0 != iReturnCode) {
            rglog.error("调用银联ATM取现操作通知服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(b2CATMCashNoticeResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(b2CATMCashNoticeResponse.getRespCode())) {
            b2CATMCashNoticeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            b2CATMCashNoticeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(b2CATMCashNoticeResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        B2CATMCashNoticeResponse b2CATMCashNoticeResponse = (B2CATMCashNoticeResponse) response.getResult();
        ConvertUtil.convertInput(b2CATMCashNoticeResponse);
    }

    private void packagePayerInfo(PayerInfo payerInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (payerInfo != null && isNotEmpty(payerInfo)) {
            StringBuffer payerInfoBuff = new StringBuffer(UpqcParamConstant.LEFT_BRACE);
            if (null != payerInfo.getAccNo() && !payerInfo.getAccNo().isEmpty()) {
                payerInfoBuff.append("accNo" + UpqcParamConstant.EQUAL + payerInfo.getAccNo() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getName() && !payerInfo.getName().isEmpty()) {
                payerInfoBuff.append("name" + UpqcParamConstant.EQUAL + payerInfo.getName() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getPayerBankInfo() && !payerInfo.getPayerBankInfo().isEmpty()) {
                payerInfoBuff.append("payerBankInfo" + UpqcParamConstant.EQUAL + payerInfo.getPayerBankInfo() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getIssCode() && !payerInfo.getIssCode().isEmpty()) {
                payerInfoBuff.append("issCode" + UpqcParamConstant.EQUAL + payerInfo.getIssCode() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCertifTp() && !payerInfo.getCertifTp().isEmpty()) {
                payerInfoBuff.append("certifTp" + UpqcParamConstant.EQUAL + payerInfo.getCertifTp() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCertifId() && !payerInfo.getCertifId().isEmpty()) {
                payerInfoBuff.append("certifId" + UpqcParamConstant.EQUAL + payerInfo.getCertifId() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCvn2() && !payerInfo.getCvn2().isEmpty()) {
                payerInfoBuff.append("cvn2" + UpqcParamConstant.EQUAL + payerInfo.getCvn2() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getExpired() && !payerInfo.getExpired().isEmpty()) {
                payerInfoBuff.append("expired" + UpqcParamConstant.EQUAL + payerInfo.getExpired() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getMobile() && !payerInfo.getMobile().isEmpty()) {
                payerInfoBuff.append("mobile" + UpqcParamConstant.EQUAL + payerInfo.getMobile() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCardAttr() && !payerInfo.getCardAttr().isEmpty()) {
                payerInfoBuff.append("cardAttr" + UpqcParamConstant.EQUAL + payerInfo.getCardAttr() + UpqcParamConstant.AMPERSAND);
            }
            payerInfoBuff.setLength(payerInfoBuff.length() - 1);
            payerInfoBuff.append(UpqcParamConstant.RIGHT_BRACE);
            xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.payInfoStr, encData(payerInfoBuff.toString()));
        }
    }
}
